package com.hex.ds.hdtp.core.app.data.contrast.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.hex.bigdata.hbda.utils.StringUtils;
import com.hex.ds.hdtp.core.app.common.entity.BasicContrastInfo;
import com.hex.ds.hdtp.core.app.common.enums.BasicListStatus;
import com.hex.ds.hdtp.core.app.common.enums.CrossDbContrastResultContrastType;
import com.hex.ds.hdtp.core.app.data.contrast.converter.ICrossDbContrastConverter;
import com.hex.ds.hdtp.core.app.data.contrast.dto.CrossDbContrastCommonDto;
import com.hex.ds.hdtp.core.app.data.contrast.dto.CrossDbContrastDataSqlDto;
import com.hex.ds.hdtp.core.app.data.contrast.dto.CrossDbContrastDataSummaryDto;
import com.hex.ds.hdtp.core.app.data.contrast.dto.CrossDbContrastSingleResultDto;
import com.hex.ds.hdtp.core.app.data.contrast.dto.request.DataCompareTaskRequestDto;
import com.hex.ds.hdtp.core.app.data.contrast.service.IContrastDataSummaryService;
import com.hex.ds.hdtp.core.app.data.contrast.service.ICrossDbContrastCommonService;
import com.hex.ds.hdtp.core.app.data.table.dto.request.TableTransferFieldMapRequestDto;
import com.hex.ds.hdtp.core.app.data.table.dto.response.TableTransferFieldMapResponseDto;
import com.hex.ds.hdtp.core.app.data.table.service.ITableTransferFieldMapService;
import com.hex.ds.hdtp.core.app.metadata.service.ISourceConfInfoService;
import com.hex.ds.hdtp.core.app.metadata.service.ISourceInfoService;
import com.hex.ds.hdtp.core.inf.data.contrast.service.IDataInfoService;
import com.hex.ds.hdtp.core.inf.data.contrast.service.impl.DataInfoSelectHandlerContext;
import com.hex.ds.hdtp.core.inf.metadata.po.SourceConfInfoPo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ContrastDataSummaryService implements IContrastDataSummaryService {

    private final DataInfoSelectHandlerContext dataInfoSelectHandlerContext;

    private final ICrossDbContrastConverter converter;

    private final ISourceConfInfoService sourceConfInfoService;

    private final ITableTransferFieldMapService tableTransferFieldMapService;

    private final ICrossDbContrastCommonService crossDbContrastCommonService;

    private final ISourceInfoService sourceInfoService;

    private static final String SOURCE_WHERE = "sourceWhereCondition";

    private static final String TARGET_WHERE = "targetWhereCondition";

    private static final String CHAR_SUM = "_CHAR_SUM";

    private static final String COLUMN_NAME = "column_name";


    private IDataInfoService getIDataInfoService(String type) {
        return dataInfoSelectHandlerContext.getDataInfoService(type);
    }

    /**
     * @Method contrastDataSummary
     * @Param contrastCommon
     * @Param contrastDataSummary
     * @Param runLogPkId
     * @Return boolean
     * @Description 数量汇总比对
     * @Author gj.xu
     * @Date 2024/1/6 19:16
     * @Version V2.0
     */
    @Override
    public boolean contrastDataSummary(CrossDbContrastCommonDto contrastCommon, CrossDbContrastDataSummaryDto contrastDataSummary, String runLogPkId) {
        try {

            // ① 有无数据（源表或对比表中任意一张表中无数据，则对比结束）
            Boolean hasDataFlag = contrastHasData(contrastCommon, contrastDataSummary, runLogPkId);

            //② 是否统计数据量-全部count
            Boolean countCompare = contrastStatisticsDataCount(contrastCommon, contrastDataSummary, runLogPkId);

            // ③ 是否统计字符length SUM
            Boolean contrastSumCharLength = contrastSumCharLength(contrastCommon, contrastDataSummary, runLogPkId);

            // ④ 字符增加trim length SUM
            Boolean contrastSumCharLengthTrim = contrastSumCharLengthTrim(contrastCommon, contrastDataSummary, runLogPkId);

            //字符length max
            Boolean contrastCharLengthMax = contrastCharLengthMax(contrastCommon, contrastDataSummary, runLogPkId);

            // ⑤ 数值SUM
            Boolean numberFiledSum = numberFiledSum(contrastCommon, contrastDataSummary, runLogPkId);
            // ① 去重count
            Boolean distinctFlag = distinctCountComparison(contrastCommon, contrastDataSummary, runLogPkId);

            // ② 分组count
            Boolean groupFlag = groupCountComparison(contrastCommon, contrastDataSummary, runLogPkId);

            // ③ 字段count
            Boolean fieldCountFlag = fieldCount(contrastCommon, contrastDataSummary, runLogPkId);

            // ④ 排除字段 -- 选择排除字段后，其他的对比中过滤掉该字段
//            excludeField(contrastCommon, contrastDataSummary, runLogPkId);

            // ⑤ 字段sum
            Boolean fieldSumFlag = sumField(contrastCommon, contrastDataSummary, runLogPkId);

            // ⑥ 字段trim
            Boolean fieldTrim = fieldTrim(contrastCommon, contrastDataSummary, runLogPkId);

            // ⑦ 例外字段
            // 暂放
//            exceptionField(dataCompareTaskRequestDto);

            // ⑧ 字段为空
            Boolean fieldIsNullCount = fieldNull(contrastCommon, contrastDataSummary, runLogPkId);

            // ⑨ 自定义SQL
            Boolean customSqlFlag = customSql(contrastCommon, contrastDataSummary, runLogPkId);
            return hasDataFlag && distinctFlag && groupFlag && fieldCountFlag && fieldSumFlag && fieldTrim && customSqlFlag
                    && countCompare && contrastSumCharLength && contrastSumCharLengthTrim && numberFiledSum && fieldIsNullCount && contrastCharLengthMax;
        } catch (Exception e) {
            log.error("汇总数据对比异常：{}", e.getMessage(), e);
            return false;
        }
    }

    /**
     * @param contrastCommon
     * @param contrastDataSummary
     * @param runLogPkId
     * @Method contrastHasData
     * @Param
     * @Return boolean
     * @Description 是否有数据
     * @Author Wang zhihao
     * @Date 2023/8/1 15:45
     * @Version V1.0
     */
    private boolean contrastHasData(CrossDbContrastCommonDto contrastCommon, CrossDbContrastDataSummaryDto contrastDataSummary, String runLogPkId) {
        if ("Y".equals(contrastDataSummary.getIsHasData())) {
            CrossDbContrastSingleResultDto singleResultDto = contrastHasData(contrastCommon, contrastDataSummary);
            if (!singleResultDto.getResult()) {
                crossDbContrastCommonService.addContrastResult(contrastCommon, runLogPkId, CrossDbContrastResultContrastType.IS_HAS_DATA.getName(),
                        BasicListStatus.FAIL.name(), singleResultDto.getMessage());
                log.error("【汇总数据对比】-[是否有数据] 对比失败");
                return false;
            }
            log.info("【汇总数据对比】-[是否有数据] 对比成功");
            crossDbContrastCommonService.addContrastResult(contrastCommon, runLogPkId, CrossDbContrastResultContrastType.IS_HAS_DATA.getName(),
                    BasicListStatus.SUCCESS.name(), singleResultDto.getMessage());
        }
        return true;
    }

    /**
     * @Method contrastStatisticsDataCount
     * @Param
     * @Param contrastCommon
     * @Param contrastDataSummary
     * @Param runLogPkId
     * @Return boolean
     * @Description 全表count
     * @Author xin.zhao
     * @Date 2023/9/22 14:20
     * @Version V1.0
     */
    private boolean contrastStatisticsDataCount(CrossDbContrastCommonDto contrastCommon, CrossDbContrastDataSummaryDto contrastDataSummary, String runLogPkId) {
        if ("Y".equals(contrastDataSummary.getIsStatisticsDataCount())) {
            CrossDbContrastSingleResultDto singleResultDto = contrastTableCount(contrastCommon, contrastDataSummary);
            if (!singleResultDto.getResult()) {
                crossDbContrastCommonService.addContrastResult(contrastCommon, runLogPkId, CrossDbContrastResultContrastType.IS_STATISTICS_DATACOUNT.getName(),
                        BasicListStatus.FAIL.name(), singleResultDto.getMessage());
                log.error("【汇总数据对比】-[全表COUNT] 对比失败");
                return false;
            }
            log.info("【汇总数据对比】-[全表COUNT] 对比成功");
            crossDbContrastCommonService.addContrastResult(contrastCommon, runLogPkId, CrossDbContrastResultContrastType.IS_STATISTICS_DATACOUNT.getName(),
                    BasicListStatus.SUCCESS.name(), singleResultDto.getMessage());
        }
        return true;
    }

    /**
     * @Method contrastSumCharLength
     * @Param
     * @Param contrastCommon
     * @Param contrastDataSummary
     * @Param runLogPkId
     * @Return boolean
     * @Description 字段长度sum
     * @Author xin.zhao
     * @Date 2023/9/26 14:16
     * @Version V1.0
     */
    private boolean contrastSumCharLength(CrossDbContrastCommonDto contrastCommon, CrossDbContrastDataSummaryDto contrastDataSummary, String runLogPkId) {
        if ("Y".equals(contrastDataSummary.getIsSumCharLength())) {
            Boolean flag = contrastFieldLengthSum(contrastCommon, contrastDataSummary);
            if (!flag) {
                crossDbContrastCommonService.addContrastResult(contrastCommon, runLogPkId, CrossDbContrastResultContrastType.IS_SUM_CHAR_LENGTH.getName(),
                        BasicListStatus.FAIL.name(), "【汇总数据对比】-[字段长度sum] 对比失败");
                log.error("【汇总数据对比】-[字段长度sum] 对比失败");
                return false;
            }
            log.info("【汇总数据对比】-[字段长度sum] 对比成功");
            crossDbContrastCommonService.addContrastResult(contrastCommon, runLogPkId, CrossDbContrastResultContrastType.IS_SUM_CHAR_LENGTH.getName(),
                    BasicListStatus.SUCCESS.name(), "");
        }
        return true;
    }

    /**
     * @Method contrastSumCharLengthTrim
     * @Param
     * @Param contrastCommon
     * @Param contrastDataSummary
     * @Param runLogPkId
     * @Return boolean
     * @Description 字段长度Trim
     * @Author xin.zhao
     * @Date 2023/9/26 14:16
     * @Version V1.0
     */
    private boolean contrastSumCharLengthTrim(CrossDbContrastCommonDto contrastCommon, CrossDbContrastDataSummaryDto contrastDataSummary, String runLogPkId) {
        if ("Y".equals(contrastDataSummary.getIsSumCharTrimLength())) {
            Boolean flag = contrastFieldLengthSumTrim(contrastCommon, contrastDataSummary);
            if (!flag) {
                crossDbContrastCommonService.addContrastResult(contrastCommon, runLogPkId, CrossDbContrastResultContrastType.IS_SUM_CHAR_TRIM_LENGTH.getName(),
                        BasicListStatus.FAIL.name(), "【汇总数据对比】-[字段长度SumTrim] 对比失败");
                log.error("【汇总数据对比】-[字段长度Trim] 对比失败");
                return false;
            }
            log.info("【汇总数据对比】-[字段长度Trim] 对比成功");
            crossDbContrastCommonService.addContrastResult(contrastCommon, runLogPkId, CrossDbContrastResultContrastType.IS_SUM_CHAR_TRIM_LENGTH.getName(),
                    BasicListStatus.SUCCESS.name(), "");
        }
        return true;
    }

    /**
     * @Method contrastCharLengthMax
     * @Param
     * @Param contrastCommon
     * @Param contrastDataSummary
     * @Param runLogPkId
     * @Return boolean
     * @Description 字符length sum
     * @Author xin.zhao
     * @Date 2023/10/18 13:59
     * @Version V1.0
     */
    private boolean contrastCharLengthMax(CrossDbContrastCommonDto contrastCommon, CrossDbContrastDataSummaryDto contrastDataSummary, String runLogPkId) {
        if ("Y".equals(contrastDataSummary.getIsMaxCharLength())) {
            Boolean flag = contrastLengthMax(contrastCommon, contrastDataSummary);
            if (!flag) {
                crossDbContrastCommonService.addContrastResult(contrastCommon, runLogPkId, CrossDbContrastResultContrastType.IS_CHAR_LENGTH_MAX.getName(),
                        BasicListStatus.FAIL.name(), "【汇总数据对比】-[字符lengthMax] 对比失败");
                log.error("【汇总数据对比】-[字符lengthMax] 对比失败");
                return false;
            }
            log.info("【汇总数据对比】-[字符lengthMax] 对比成功");
            crossDbContrastCommonService.addContrastResult(contrastCommon, runLogPkId, CrossDbContrastResultContrastType.IS_CHAR_LENGTH_MAX.getName(),
                    BasicListStatus.SUCCESS.name(), "");
        }
        return true;
    }

    /**
     * @Method numberFiledSum
     * @Param
     * @Param contrastCommon
     * @Param contrastDataSummary
     * @Param runLogPkId
     * @Return boolean
     * @Description 数字字符sum
     * @Author xin.zhao
     * @Date 2023/9/26 14:16
     * @Version V1.0
     */
    private boolean numberFiledSum(CrossDbContrastCommonDto contrastCommon, CrossDbContrastDataSummaryDto contrastDataSummary, String runLogPkId) {
        if ("Y".equals(contrastDataSummary.getIsStatisticsNumberSum())) {
            Boolean flag = contrastNumberSum(contrastCommon, contrastDataSummary);
            if (!flag) {
                crossDbContrastCommonService.addContrastResult(contrastCommon, runLogPkId, CrossDbContrastResultContrastType.IS_STATISTICS_NUMBER_SUM.getName(),
                        BasicListStatus.FAIL.name(), "【汇总数据对比】-[数字字段Sum] 对比失败");
                log.error("【汇总数据对比】-[数字字段Sum] 对比失败");
                return false;
            }
            log.info("【汇总数据对比】-[数字字段Sum] 对比成功");
            crossDbContrastCommonService.addContrastResult(contrastCommon, runLogPkId, CrossDbContrastResultContrastType.IS_STATISTICS_NUMBER_SUM.getName(),
                    BasicListStatus.SUCCESS.name(), "");
        }
        return true;
    }

    /**
     * @Method distinctCountComparison
     * @Param [dataCompareTaskRequestDto, dataCompareTaskResponseDtoList]
     * @Return boolean
     * @Description 去重count对比
     * @Author Wang zhihao
     * @Date 2023/7/19 11:41
     * @Version V1.0
     */
    private boolean distinctCountComparison(CrossDbContrastCommonDto contrastCommon, CrossDbContrastDataSummaryDto contrastDataSummary, String runLogPkId) {
        if (!CollUtil.isEmpty(contrastDataSummary.getRemoveRepeatCountField())) {
            BasicContrastInfo contrastInfo = converter.toBasicContrastInfo(contrastCommon);
            // 根据比对项所关联的表迁移的字段映射获取比对源所对应的字段
            List<String> queryField = contrastDataSummary.getRemoveRepeatCountField().stream().map(e -> {
                String fieldNameByMap = getFieldNameByMap(e, contrastCommon);
                return fieldNameByMap;
            }).collect(Collectors.toList());
            contrastInfo.setField(contrastDataSummary.getRemoveRepeatCountField());
            contrastInfo.setContrastField(queryField);
            CrossDbContrastSingleResultDto singleResultDto = distinctCountComparison(contrastInfo, contrastDataSummary);
            if (!singleResultDto.getResult()) {
                crossDbContrastCommonService.addContrastResult(contrastCommon, runLogPkId, CrossDbContrastResultContrastType.REMOVE_REPEAT_COUNT_FIELD.getName(),
                        BasicListStatus.FAIL.name(), singleResultDto.getMessage());
                log.error("【汇总数据对比】-[指定字段去重count] 对比失败");
                return false;
            }
            log.info("【汇总数据对比】-[指定字段去重count] 对比成功");
            crossDbContrastCommonService.addContrastResult(contrastCommon, runLogPkId, CrossDbContrastResultContrastType.REMOVE_REPEAT_COUNT_FIELD.getName(),
                    BasicListStatus.SUCCESS.name(), singleResultDto.getMessage());
        }
        return true;
    }

    /**
     * @Method groupCountComparison
     * @Param [dataCompareTaskRequestDto]
     * @Return boolean
     * @Description 分组count对比
     * @Author Wang zhihao
     * @Date 2023/7/19 11:46
     * @Version V1.0
     */
    private boolean groupCountComparison(CrossDbContrastCommonDto contrastCommon, CrossDbContrastDataSummaryDto contrastDataSummary, String runLogPkId) {
        if (!CollUtil.isEmpty(contrastDataSummary.getGroupCountField())) {
            BasicContrastInfo contrastInfo = converter.toBasicContrastInfo(contrastCommon);
            // 根据比对项所关联的表迁移的字段映射获取比对源所对应的字段
            List<String> queryField = contrastDataSummary.getGroupCountField().stream().map(e -> {
                String fieldNameByMap = getFieldNameByMap(e, contrastCommon);
                return fieldNameByMap;
            }).collect(Collectors.toList());
            contrastInfo.setField(contrastDataSummary.getGroupCountField());
            contrastInfo.setContrastField(queryField);
            CrossDbContrastSingleResultDto singleResultDto = groupCountComparison(contrastInfo, contrastDataSummary);
            if (!singleResultDto.getResult()) {
                crossDbContrastCommonService.addContrastResult(contrastCommon, runLogPkId, CrossDbContrastResultContrastType.GROUP_COUNT_FIELD.getName(),
                        BasicListStatus.FAIL.name(), singleResultDto.getMessage());
                log.error("【汇总数据对比】-[指定分组字段count] 对比失败");
                return false;
            }
            log.info("【汇总数据对比】-[指定分组字段count] 对比成功");
            crossDbContrastCommonService.addContrastResult(contrastCommon, runLogPkId, CrossDbContrastResultContrastType.GROUP_COUNT_FIELD.getName(),
                    BasicListStatus.SUCCESS.name(), singleResultDto.getMessage());
        }
        return true;
    }

    /**
     * @Method fieldCount
     * @Param [dataCompareTaskRequestDto]
     * @Return boolean
     * @Description 字段count对比
     * @Author Wang zhihao
     * @Date 2023/7/19 11:48
     * @Version V1.0
     */
    private boolean fieldCount(CrossDbContrastCommonDto contrastCommon, CrossDbContrastDataSummaryDto contrastDataSummary, String runLogPkId) {
        if (!CollUtil.isEmpty(contrastDataSummary.getCountField())) {
            BasicContrastInfo contrastInfo = converter.toBasicContrastInfo(contrastCommon);
            // 根据比对项所关联的表迁移的字段映射获取比对源所对应的字段
            List<String> queryField = contrastDataSummary.getCountField().stream().map(e -> {
                String fieldNameByMap = getFieldNameByMap(e, contrastCommon);
                return fieldNameByMap;
            }).collect(Collectors.toList());
            contrastInfo.setField(contrastDataSummary.getCountField());
            contrastInfo.setContrastField(queryField);
            CrossDbContrastSingleResultDto singleResultDto = fieldCount(contrastInfo, contrastDataSummary);
            if (!singleResultDto.getResult()) {
                crossDbContrastCommonService.addContrastResult(contrastCommon, runLogPkId, CrossDbContrastResultContrastType.COUNT_FIELD.getName(),
                        BasicListStatus.FAIL.name(), singleResultDto.getMessage());
                log.error("【汇总数据对比】-[指定分组字段count] 对比失败");
                return false;
            }
            log.info("【汇总数据对比】-[指定分组字段count] 对比成功");
            crossDbContrastCommonService.addContrastResult(contrastCommon, runLogPkId, CrossDbContrastResultContrastType.COUNT_FIELD.getName(),
                    BasicListStatus.SUCCESS.name(), singleResultDto.getMessage());
        }
        return true;
    }

    /**
     * @Method sumField
     * @Param [dataCompareTaskRequestDto]
     * @Return boolean
     * @Description 字段sum对比
     * @Author Wang zhihao
     * @Date 2023/7/19 20:21
     * @Version V1.0
     */
    private boolean sumField(CrossDbContrastCommonDto contrastCommon, CrossDbContrastDataSummaryDto contrastDataSummary, String runLogPkId) {
        if (!CollUtil.isEmpty(contrastDataSummary.getSumField())) {
            BasicContrastInfo contrastInfo = converter.toBasicContrastInfo(contrastCommon);
            // 根据比对项所关联的表迁移的字段映射获取比对源所对应的字段
            List<String> queryField = contrastDataSummary.getSumField().stream().map(e -> {
                String fieldNameByMap = getFieldNameByMap(e, contrastCommon);
                return fieldNameByMap;
            }).collect(Collectors.toList());
            contrastInfo.setField(contrastDataSummary.getSumField());
            contrastInfo.setContrastField(queryField);
            CrossDbContrastSingleResultDto singleResultDto = sumField(contrastInfo, contrastDataSummary);
            if (!singleResultDto.getResult()) {
                crossDbContrastCommonService.addContrastResult(contrastCommon, runLogPkId, CrossDbContrastResultContrastType.SUM_FIELD.getName(),
                        BasicListStatus.FAIL.name(), singleResultDto.getMessage());
                log.error("【汇总数据对比】-[指定字段sum] 对比失败");
                return false;
            }
            log.info("【汇总数据对比】-[指定字段sum] 对比成功");
            crossDbContrastCommonService.addContrastResult(contrastCommon, runLogPkId, CrossDbContrastResultContrastType.SUM_FIELD.getName(),
                    BasicListStatus.SUCCESS.name(), singleResultDto.getMessage());
        }
        return true;
    }

    /**
     * @Method fieldTrim
     * @Param [dataCompareTaskRequestDto]
     * @Return boolean
     * @Description 字段trim对比
     * @Author Wang zhihao
     * @Date 2023/7/19 20:21
     * @Version V1.0
     */
    private boolean fieldTrim(CrossDbContrastCommonDto contrastCommon, CrossDbContrastDataSummaryDto contrastDataSummary, String runLogPkId) {
        if (!CollUtil.isEmpty(contrastDataSummary.getTrimField())) {
            BasicContrastInfo contrastInfo = converter.toBasicContrastInfo(contrastCommon);
            // 根据比对项所关联的表迁移的字段映射获取比对源所对应的字段
            List<String> queryField = contrastDataSummary.getTrimField().stream().map(e -> {
                String fieldNameByMap = getFieldNameByMap(e, contrastCommon);
                return fieldNameByMap;
            }).collect(Collectors.toList());
            contrastInfo.setField(contrastDataSummary.getTrimField());
            contrastInfo.setContrastField(queryField);
            CrossDbContrastSingleResultDto singleResultDto = fieldTrim(contrastInfo, contrastDataSummary);
            if (!singleResultDto.getResult()) {
                crossDbContrastCommonService.addContrastResult(contrastCommon, runLogPkId, CrossDbContrastResultContrastType.TRIM_FIELD.getName(),
                        BasicListStatus.FAIL.name(), singleResultDto.getMessage());
                log.error("【汇总数据对比】-[指定字段trim] 对比失败");
                return false;
            }
            log.info("【汇总数据对比】-[指定字段trim] 对比成功");
            crossDbContrastCommonService.addContrastResult(contrastCommon, runLogPkId, CrossDbContrastResultContrastType.TRIM_FIELD.getName(),
                    BasicListStatus.SUCCESS.name(), singleResultDto.getMessage());
        }
        return true;
    }

    /**
     * @Method exceptionField
     * @Param [dataCompareTaskRequestDto]
     * @Return void
     * @Description 例外字段对比
     * @Author Wang zhihao
     * @Date 2023/7/20 9:33
     * @Version V1.0
     */
    private void exceptionField(DataCompareTaskRequestDto dataCompareTaskRequestDto) {
        try {

        } catch (Exception e) {
            log.error("例外字段对比异常" + e);
        }
    }

    /**
     * @Method fieldNull
     * @Param [dataCompareTaskRequestDto]
     * @Return void
     * @Description 字段为空对比
     * @Author Wang zhihao
     * @Date 2023/7/20 9:37
     * @Version V1.0
     */
    private Boolean fieldNull(CrossDbContrastCommonDto contrastCommon, CrossDbContrastDataSummaryDto contrastDataSummary, String runLogPkId) {
        if (!CollUtil.isEmpty(contrastDataSummary.getEmptyField())) {
            BasicContrastInfo contrastInfo = converter.toBasicContrastInfo(contrastCommon);
            // 根据比对项所关联的表迁移的字段映射获取比对源所对应的字段
            List<String> queryField = contrastDataSummary.getEmptyField().stream().map(e -> {
                String fieldNameByMap = getFieldNameByMap(e, contrastCommon);
                return fieldNameByMap;
            }).collect(Collectors.toList());
            contrastInfo.setField(contrastDataSummary.getEmptyField());
            contrastInfo.setContrastField(queryField);
            //字段为空具体比对逻辑
            CrossDbContrastSingleResultDto singleResultDto = fieldCountIsNull(contrastInfo, contrastDataSummary);
            if (!singleResultDto.getResult()) {
                crossDbContrastCommonService.addContrastResult(contrastCommon, runLogPkId, CrossDbContrastResultContrastType.REMOVE_REPEAT_COUNT_FIELD.getName(),
                        BasicListStatus.FAIL.name(), singleResultDto.getMessage());
                log.error("【汇总数据对比】-[字段为空] 对比失败");
                return false;
            }
            log.info("【汇总数据对比】-[字段为空] 对比成功");
            crossDbContrastCommonService.addContrastResult(contrastCommon, runLogPkId, CrossDbContrastResultContrastType.REMOVE_REPEAT_COUNT_FIELD.getName(),
                    BasicListStatus.SUCCESS.name(), singleResultDto.getMessage());
        }
        return true;
    }

    /**
     * @Method customSQL
     * @Param [dataCompareTaskRequestDto]
     * @Return void
     * @Description 自定义SQL对比
     * @Author Wang zhihao
     * @Date 2023/7/20 9:39
     * * @Version V1.0
     */
    private boolean customSql(CrossDbContrastCommonDto contrastCommon, CrossDbContrastDataSummaryDto contrastDataSummary, String runLogPkId) {
        try {
            if (!StringUtils.isEmpty(contrastDataSummary.getCustomSql())) {
                Boolean flag = contrastCustomSql(converter.toBasicContrastInfo(contrastCommon), contrastDataSummary);
                if (!flag) {
                    log.error("【数据统计比对】-[自定义sql] 对比失败");
                    crossDbContrastCommonService.addContrastResult(contrastCommon, runLogPkId, CrossDbContrastResultContrastType.CUSTOM_SQL.getName(),
                            BasicListStatus.FAIL.name(), "【数据统计比对】-[自定义sql] 对比失败");
                    return false;
                }
                log.info("【数据统计比对】-[自定义sql] 对比成功");
                crossDbContrastCommonService.addContrastResult(contrastCommon, runLogPkId, CrossDbContrastResultContrastType.CUSTOM_SQL.getName(),
                        BasicListStatus.SUCCESS.name(), "");
            }
        } catch (Exception e) {
            log.error("自定义SQL对比异常" + e);
        }
        return true;
    }

    public boolean contrastCustomSql(BasicContrastInfo contrastInfo, CrossDbContrastDataSummaryDto contrastDataSummary) {
        Boolean flag = false;
        try {
            List<SourceConfInfoPo> sourceConfInfoPoList = sourceConfInfoService.queryListBySourceCode(contrastInfo.getSourceCode());
            List<SourceConfInfoPo> contrastConfInfoPoList = sourceConfInfoService.queryListBySourceCode(contrastInfo.getContrastCode());
            JSONObject jsonObject = JSONObject.parseObject(contrastDataSummary.getCustomSql());
            String sourceSql = jsonObject.getString("sourceCustomSql").replace(";", "");
            String targetSql = jsonObject.getString("targetCustomSql").replace(";", "");
            //查询数据
            List<Map<String, Object>> sourceList = getIDataInfoService(sourceConfInfoPoList.get(0).getSourceType()).selectCustomSql(contrastInfo.getSourceCode(), sourceSql);
            List<Map<String, Object>> targetList = getIDataInfoService(contrastConfInfoPoList.get(0).getSourceType()).selectCustomSql(contrastInfo.getContrastCode(), targetSql);
            if (sourceList.size() == targetList.size()) {
                flag = true;
            }

        } catch (Exception e) {
            log.error("自定义SQL对比异常" + e);
        }
        return flag;
    }

    /**
     * @param contrastInfo
     * @Method contrastHasData
     * @Param
     * @Return java.lang.Boolean
     * @Description 表中是否有数据
     * @Author Wang zhihao
     * @Date 2023/8/17 19:58
     * @Version V1.0
     */
    @Override
    public CrossDbContrastSingleResultDto contrastHasData(CrossDbContrastCommonDto contrastInfo, CrossDbContrastDataSummaryDto contrastDataSummary) {
        try {
            //获取源和目标库表信息
            List<SourceConfInfoPo> sourceConfInfoPoList = sourceConfInfoService.queryListBySourceCode(contrastInfo.getSourceCode());
            List<SourceConfInfoPo> contrastConfInfoPoList = sourceConfInfoService.queryListBySourceCode(contrastInfo.getContrastCode());
            String sourceType = sourceConfInfoPoList.get(0).getSourceType();
            String contrastType = contrastConfInfoPoList.get(0).getSourceType();
            CrossDbContrastDataSqlDto dataSqlDto = crossDbContrastCommonService.buildPartitionWhereSql(sourceType, contrastType, contrastInfo);
            String sourceWhereCondition = initWhereCondition(dataSqlDto, contrastDataSummary, SOURCE_WHERE);
            String targetWhereCondition = initWhereCondition(dataSqlDto, contrastDataSummary, TARGET_WHERE);
            //查询表中的数据量
            Long sourceInfoNum = getIDataInfoService(sourceType)
                    .selectDataNum(contrastInfo.getSourceCode(), contrastInfo.getSourceSchema(), contrastInfo.getSourceTable(), sourceWhereCondition, "");
            Long targetInfoNum = getIDataInfoService(contrastType)
                    .selectDataNum(contrastInfo.getContrastCode(), contrastInfo.getContrastSchema(), contrastInfo.getContrastTable(), targetWhereCondition, "");
            if (!sourceInfoNum.equals(0L) && !targetInfoNum.equals(0L)) {
                return new CrossDbContrastSingleResultDto(true, "两表均存在数据");
            } else {
                return new CrossDbContrastSingleResultDto(false, "源表数据量" + (!sourceInfoNum.equals(0L) ? sourceInfoNum.toString() : "为0") + " 目标表数据量" + (!targetInfoNum.equals(0L) ? targetInfoNum.toString() : "为0"));
            }
        } catch (Exception e) {
            Throwable rootCause = crossDbContrastCommonService.findRootCause(e);
            log.error("表中是否有数据比对发生异常: {}", e.getMessage(), e);
            return new CrossDbContrastSingleResultDto(false, rootCause.getMessage());
        }
    }

    private String initWhereCondition(CrossDbContrastDataSqlDto dataSqlDto, CrossDbContrastDataSummaryDto contrastDataSummary, String type) throws SQLException {
        String whereCondition = "";
        if (!StrUtil.isEmpty(contrastDataSummary.getWhereCondition())) {
            JSONObject jsonObject = JSONObject.parseObject(contrastDataSummary.getWhereCondition());
            whereCondition = jsonObject.getString(type).replace(";", "");
        }
        return getWhereSql(dataSqlDto.getSourcePartitionWhereSql(), whereCondition);
    }


    /**
     * @Method contrastFieldLengthWithMap
     * @Param
     * @Param contrastInfo
     * @Return java.lang.Boolean
     * @Description 全表COUNT对比
     * @Author xin.zhao
     * @Date 2023/9/22 16:47
     * @Version V1.0
     */
    public CrossDbContrastSingleResultDto contrastTableCount(CrossDbContrastCommonDto contrastInfo, CrossDbContrastDataSummaryDto contrastDataSummary) {
        try {
            List<SourceConfInfoPo> sourceConfInfoPoList = sourceConfInfoService.queryListBySourceCode(contrastInfo.getSourceCode());
            List<SourceConfInfoPo> contrastConfInfoPoList = sourceConfInfoService.queryListBySourceCode(contrastInfo.getContrastCode());
            String sourceType = sourceConfInfoPoList.get(0).getSourceType();
            String contrastType = contrastConfInfoPoList.get(0).getSourceType();
            CrossDbContrastDataSqlDto dataSqlDto = crossDbContrastCommonService.buildPartitionWhereSql(sourceType, contrastType, contrastInfo);
            String sourceWhereCondition = initWhereCondition(dataSqlDto, contrastDataSummary, SOURCE_WHERE);
            String targetWhereCondition = initWhereCondition(dataSqlDto, contrastDataSummary, TARGET_WHERE);
            //查询表中的数据量
            Long sourceInfoNum = getIDataInfoService(sourceType)
                    .selectDataNum(contrastInfo.getSourceCode(), contrastInfo.getSourceSchema(), contrastInfo.getSourceTable(), sourceWhereCondition, "");
            Long targetInfoNum = getIDataInfoService(contrastType)
                    .selectDataNum(contrastInfo.getContrastCode(), contrastInfo.getContrastSchema(), contrastInfo.getContrastTable(), targetWhereCondition, "");
            if (sourceInfoNum.equals(targetInfoNum)) {
                return new CrossDbContrastSingleResultDto(true, "两表数据量相同，数据量为：" + sourceInfoNum);
            }
            return new CrossDbContrastSingleResultDto(false, "原表数据量为：" + sourceInfoNum + ", 目标表数据量为:" + targetInfoNum);
        } catch (Exception e) {
            Throwable rootCause = crossDbContrastCommonService.findRootCause(e);
            log.error("全表count比对: {}", e.getMessage(), e);
            return new CrossDbContrastSingleResultDto(false, rootCause.getMessage());
        }
    }

    /**
     * @Method contrastTableCount
     * @Param
     * @Param contrastInfo
     * @Return java.lang.Boolean
     * @Description 字符类型Sum
     * @Author xin.zhao
     * @Date 2023/9/25 9:42
     * @Version V1.0
     */
    public Boolean contrastFieldLengthSum(CrossDbContrastCommonDto contrastInfo, CrossDbContrastDataSummaryDto contrastDataSummary) {
        try {
            Long sourceInfoNum = null;
            Long targetInfoNum = null;
            Boolean flag = true;
            List<SourceConfInfoPo> sourceConfInfoPoList = sourceConfInfoService.queryListBySourceCode(contrastInfo.getSourceCode());
            List<SourceConfInfoPo> contrastConfInfoPoList = sourceConfInfoService.queryListBySourceCode(contrastInfo.getContrastCode());
            String sourceType = sourceConfInfoPoList.get(0).getSourceType();
            String contrastType = contrastConfInfoPoList.get(0).getSourceType();
            //获取 全列
            List<String> fieldList = getIDataInfoService(sourceType)
                    .selectFieldName(contrastInfo.getSourceCode(), contrastInfo.getSourceSchema(), contrastInfo.getSourceTable());
            //获取源到目标源字段映射
            List<TableTransferFieldMapResponseDto> tableTransferFieldMaps = new ArrayList<>();
            if (contrastInfo.getTableTransferPkId() != null) {
                //如果经历过表迁移，那么就从表迁移里获取字段映射
                TableTransferFieldMapRequestDto tableTransferFieldMapRequestDto = new TableTransferFieldMapRequestDto();
                tableTransferFieldMapRequestDto.setListPkId(contrastInfo.getTableTransferPkId());
                tableTransferFieldMaps = tableTransferFieldMapService.queryList(tableTransferFieldMapRequestDto);
            } else {
                //否则就连接表获取字段映射
                tableTransferFieldMaps = sourceInfoService.getFieldMaps(converter.toBasicTransferInfo(contrastInfo));
            }
            List<String> excludeFields = contrastDataSummary.getExcludeFields();
            if (!CollUtil.isEmpty(excludeFields)) {
                fieldList = fieldList.stream().filter(field -> !excludeFields.contains(field)).collect(Collectors.toList());
            }
            //获取字段映射
            Map<String, String> fieldsMap = tableTransferFieldMaps.stream()
                    .collect(Collectors.toMap(TableTransferFieldMapResponseDto::getSourceFieldNameUpper, TableTransferFieldMapResponseDto::getTargetFieldName));
            Map<String, String> fieldTypeMap = tableTransferFieldMaps.stream()
                    .collect(Collectors.toMap(TableTransferFieldMapResponseDto::getTargetFieldNameUpper, TableTransferFieldMapResponseDto::getSourceFieldType));
            // 获取源的字符sum字段
            List<String> sumFields = getIDataInfoService(sourceType).getStrDataType(sourceType);
            CrossDbContrastDataSqlDto dataSqlDto = crossDbContrastCommonService.buildPartitionWhereSql(sourceType, contrastType, contrastInfo);
            String sourceWhereCondition = initWhereCondition(dataSqlDto, contrastDataSummary, SOURCE_WHERE);
            String targetWhereCondition = initWhereCondition(dataSqlDto, contrastDataSummary, TARGET_WHERE);
            for (String field : fieldList) {
                if (sumFields.contains(fieldTypeMap.get(field.toUpperCase()).toUpperCase())) {
                    sourceInfoNum = getIDataInfoService(sourceType)
                            .selectFieldLengthSum(contrastInfo.getSourceCode(), field, contrastInfo.getSourceSchema(), contrastInfo.getSourceTable(), sourceWhereCondition);
                    String targetField = fieldsMap.get(field.toUpperCase());
                    targetInfoNum = getIDataInfoService(contrastType)
                            .selectFieldLengthSum(contrastInfo.getContrastCode(), targetField, contrastInfo.getContrastSchema(), contrastInfo.getContrastTable(), targetWhereCondition);
                    log.info("对" + field + "字段进行sum" + "比较结果为" + (sourceInfoNum.equals(targetInfoNum) == true ? "成功" : "失败"));
                    flag = sourceInfoNum.equals(targetInfoNum);
                    if (!flag) break;
                }
            }
            return flag;
        } catch (Exception e) {
            log.error("字符类型sum: {}", e.getMessage(), e);
            return false;
        }
    }

    /**
     * @Method contrastTableCount
     * @Param
     * @Param contrastInfo
     * @Return java.lang.Boolean
     * @Description 字符类型Sum Trim
     * @Author xin.zhao
     * @Date 2023/9/25 9:42
     * @Version V1.0
     */
    public Boolean contrastFieldLengthSumTrim(CrossDbContrastCommonDto contrastInfo, CrossDbContrastDataSummaryDto contrastDataSummary) {
        try {
            Long sourceInfoNum = null;
            Long targetInfoNum = null;
            Boolean flag = true;
            List<SourceConfInfoPo> sourceConfInfoPoList = sourceConfInfoService.queryListBySourceCode(contrastInfo.getSourceCode());
            List<SourceConfInfoPo> contrastConfInfoPoList = sourceConfInfoService.queryListBySourceCode(contrastInfo.getContrastCode());
            String sourceType = sourceConfInfoPoList.get(0).getSourceType();
            String contrastType = contrastConfInfoPoList.get(0).getSourceType();
            //获取 全列
            List<String> fieldList = getIDataInfoService(sourceType)
                    .selectFieldName(contrastInfo.getSourceCode(), contrastInfo.getSourceSchema(), contrastInfo.getSourceTable());
            //获取初始源到目标源字段映射
            List<TableTransferFieldMapResponseDto> tableTransferFieldMaps = new ArrayList<>();
            if (contrastInfo.getTableTransferPkId() != null) {
                //如果经历过表迁移，那么就从表迁移里获取字段映射
                TableTransferFieldMapRequestDto tableTransferFieldMapRequestDto = new TableTransferFieldMapRequestDto();
                tableTransferFieldMapRequestDto.setListPkId(contrastInfo.getTableTransferPkId());
                tableTransferFieldMaps = tableTransferFieldMapService.queryList(tableTransferFieldMapRequestDto);
            } else {
                //否则就连接表获取字段映射
                tableTransferFieldMaps = sourceInfoService.getFieldMaps(converter.toBasicTransferInfo(contrastInfo));
            }
            List<String> excludeFields = contrastDataSummary.getExcludeFields();
            if (!CollUtil.isEmpty(excludeFields)) {
                fieldList = fieldList.stream().filter(field -> !excludeFields.contains(field)).collect(Collectors.toList());
            }
            //获取字段映射
            Map<String, String> fieldsMap = tableTransferFieldMaps.stream()
                    .collect(Collectors.toMap(TableTransferFieldMapResponseDto::getSourceFieldNameUpper, TableTransferFieldMapResponseDto::getTargetFieldName));
            Map<String, String> fieldTypeMap = tableTransferFieldMaps.stream()
                    .collect(Collectors.toMap(TableTransferFieldMapResponseDto::getSourceFieldNameUpper, TableTransferFieldMapResponseDto::getSourceFieldType));

            // 获取源的字符sum字段
            List<String> sumFields = getIDataInfoService(sourceType).getStrDataType(sourceType);
            CrossDbContrastDataSqlDto dataSqlDto = crossDbContrastCommonService.buildPartitionWhereSql(sourceType, contrastType, contrastInfo);
            String sourceWhereCondition = initWhereCondition(dataSqlDto, contrastDataSummary, SOURCE_WHERE);
            String targetWhereCondition = initWhereCondition(dataSqlDto, contrastDataSummary, TARGET_WHERE);
            for (String field : fieldList) {
                if (sumFields.contains(fieldTypeMap.get(field.toUpperCase()).toUpperCase())) {
                    sourceInfoNum = getIDataInfoService(sourceType)
                            .selectFieldLengthSumTrim(contrastInfo.getSourceCode(), field, contrastInfo.getSourceSchema(), contrastInfo.getSourceTable(), sourceWhereCondition);
                    String targetField = fieldsMap.get(field.toUpperCase());
                    targetInfoNum = getIDataInfoService(contrastType)
                            .selectFieldLengthSumTrim(contrastInfo.getContrastCode(), targetField, contrastInfo.getContrastSchema(), contrastInfo.getContrastTable(), targetWhereCondition);
                    log.info("对" + field + "字段" + "比较结果为" + (sourceInfoNum.equals(targetInfoNum) == true ? "成功" : "失败"));
                    flag = sourceInfoNum.equals(targetInfoNum);
                    if (!flag) break;
                }
            }
            return flag;
        } catch (Exception e) {
            log.error("字符类型sum Trim: {}", e.getMessage(), e);
            return false;
        }
    }

    public Boolean contrastLengthMax(CrossDbContrastCommonDto contrastInfo, CrossDbContrastDataSummaryDto contrastDataSummary) {
        try {
            Boolean flag = true;
            List<SourceConfInfoPo> sourceConfInfoPoList = sourceConfInfoService.queryListBySourceCode(contrastInfo.getSourceCode());
            List<SourceConfInfoPo> targetConfInfoPoList = sourceConfInfoService.queryListBySourceCode(contrastInfo.getContrastCode());
            String sourceType = sourceConfInfoPoList.get(0).getSourceType();
            String contrastType = targetConfInfoPoList.get(0).getSourceType();
            //获取 全列
            List<String> fieldList = getIDataInfoService(sourceType)
                    .selectFieldName(contrastInfo.getSourceCode(), contrastInfo.getSourceSchema(), contrastInfo.getSourceTable());
            //获取源表到目标表字段映射
            List<TableTransferFieldMapResponseDto> tableTransferFieldMaps = new ArrayList<>();
            if (contrastInfo.getTableTransferPkId() != null) {
                //如果经历过表迁移，那么就从表迁移里获取字段映射
                TableTransferFieldMapRequestDto tableTransferFieldMapRequestDto = new TableTransferFieldMapRequestDto();
                tableTransferFieldMapRequestDto.setListPkId(contrastInfo.getTableTransferPkId());
                tableTransferFieldMaps = tableTransferFieldMapService.queryList(tableTransferFieldMapRequestDto);
            } else {
                //否则就连接表获取字段映射
                tableTransferFieldMaps = sourceInfoService.getFieldMaps(converter.toBasicTransferInfo(contrastInfo));
            }
            //获取字段映射
            Map<String, String> fieldsNameMap = tableTransferFieldMaps.stream()
                    .collect(Collectors.toMap(TableTransferFieldMapResponseDto::getSourceFieldNameUpper, TableTransferFieldMapResponseDto::getTargetFieldName));
            Map<String, String> fieldTypeMap = tableTransferFieldMaps.stream()
                    .collect(Collectors.toMap(TableTransferFieldMapResponseDto::getSourceFieldNameUpper, TableTransferFieldMapResponseDto::getSourceFieldType));
            //排除字段
            List<String> excludeFields = contrastDataSummary.getExcludeFields();
            if (!CollUtil.isEmpty(excludeFields)) {
                fieldList = fieldList.stream().filter(field -> !excludeFields.contains(field)).collect(Collectors.toList());
            }
            // 获取源的 可 length 字段
            List<String> sumFields = getIDataInfoService(sourceType).getStrDataType(sourceType);
            CrossDbContrastDataSqlDto dataSqlDto = crossDbContrastCommonService.buildPartitionWhereSql(sourceType, contrastType, contrastInfo);
            String sourceWhereCondition = "";
            String targetWhereCondition = "";
            if (!StrUtil.isEmpty(contrastDataSummary.getWhereCondition())) {
                JSONObject jsonObject = JSONObject.parseObject(contrastDataSummary.getWhereCondition());
                sourceWhereCondition = jsonObject.getString(SOURCE_WHERE).replace(";", "");
                targetWhereCondition = jsonObject.getString(TARGET_WHERE).replace(";", "");
            }
            sourceWhereCondition = getWhereSql(dataSqlDto.getSourcePartitionWhereSql(), sourceWhereCondition);
            targetWhereCondition = getWhereSql(dataSqlDto.getSourcePartitionWhereSql(), targetWhereCondition);
            for (String field : fieldList) {
                if (sumFields.contains(fieldTypeMap.get(field.toUpperCase()).toUpperCase())) {
                    Long sourceInfoLength = getIDataInfoService(sourceType)
                            .selectLengthMax(contrastInfo.getSourceCode(), contrastInfo.getSourceSchema(), contrastInfo.getSourceTable(), field, sourceWhereCondition);
                    String targetField = fieldsNameMap.get(field.toUpperCase());
                    Long targetInfoLength = getIDataInfoService(contrastType)
                            .selectLengthMax(contrastInfo.getContrastCode(), contrastInfo.getContrastSchema(), contrastInfo.getContrastTable(), targetField, targetWhereCondition);
                    log.info("对" + field + "字段sum比较结果为" + (sourceInfoLength.equals(targetInfoLength) == true ? "成功" : "失败"));
                    flag = sourceInfoLength.equals(targetInfoLength);
                    if (!flag) break;
                }
            }
            return flag;
        } catch (Exception e) {
            log.error("lengthMax：{}", e.getMessage(), e);
            return false;
        }
    }

    /**
     * @Method contrastNumberSum
     * @Param
     * @Param contrastInfo
     * @Param transferListPkId
     * @Return java.lang.Boolean
     * @Description 数值sum
     * @Author xin.zhao
     * @Date 2023/9/25 17:13
     * @Version V1.0
     */
    public Boolean contrastNumberSum(CrossDbContrastCommonDto contrastInfo, CrossDbContrastDataSummaryDto contrastDataSummary) {
        try {
            Float sourceInfoNum = null;
            Float targetInfoNum = null;
            Boolean flag = true;
            List<SourceConfInfoPo> sourceConfInfoPoList = sourceConfInfoService.queryListBySourceCode(contrastInfo.getSourceCode());
            List<SourceConfInfoPo> contrastConfInfoPoList = sourceConfInfoService.queryListBySourceCode(contrastInfo.getContrastCode());
            String sourceType = sourceConfInfoPoList.get(0).getSourceType();
            String contrastType = contrastConfInfoPoList.get(0).getSourceType();
            //获取 全列
            List<String> fieldList = getIDataInfoService(sourceType)
                    .selectFieldName(contrastInfo.getSourceCode(), contrastInfo.getSourceSchema(), contrastInfo.getSourceTable());
            //获取源表到目标表字段映射
            List<TableTransferFieldMapResponseDto> tableTransferFieldMaps = new ArrayList<>();
            if (contrastInfo.getTableTransferPkId() != null) {
                //如果经历过表迁移，那么就从表迁移里获取字段映射
                TableTransferFieldMapRequestDto tableTransferFieldMapRequestDto = new TableTransferFieldMapRequestDto();
                tableTransferFieldMapRequestDto.setListPkId(contrastInfo.getTableTransferPkId());
                tableTransferFieldMaps = tableTransferFieldMapService.queryList(tableTransferFieldMapRequestDto);
            } else {
                //否则就连接表获取字段映射
                tableTransferFieldMaps = sourceInfoService.getFieldMaps(converter.toBasicTransferInfo(contrastInfo));
            }
            //获取字段映射
            Map<String, String> fieldsNameMap = tableTransferFieldMaps.stream()
                    .collect(Collectors.toMap(TableTransferFieldMapResponseDto::getSourceFieldNameUpper, TableTransferFieldMapResponseDto::getTargetFieldName));
            Map<String, String> fieldTypeMap = tableTransferFieldMaps.stream()
                    .collect(Collectors.toMap(TableTransferFieldMapResponseDto::getSourceFieldNameUpper, TableTransferFieldMapResponseDto::getSourceFieldType));
            List<String> excludeFields = contrastDataSummary.getExcludeFields();
            if (!CollUtil.isEmpty(excludeFields)) {
                fieldList = fieldList.stream().filter(field -> !excludeFields.contains(field)).collect(Collectors.toList());
            }
            // 获取源的数值sum数据类型
            List<String> sumFields = getIDataInfoService(sourceType).getNumDataType(sourceType);
            CrossDbContrastDataSqlDto dataSqlDto = crossDbContrastCommonService.buildPartitionWhereSql(sourceType, contrastType, contrastInfo);
            String sourceWhereCondition = initWhereCondition(dataSqlDto, contrastDataSummary, SOURCE_WHERE);
            String targetWhereCondition = initWhereCondition(dataSqlDto, contrastDataSummary, TARGET_WHERE);
            for (String field : fieldList) {
                if (sumFields.contains(fieldTypeMap.get(field.toUpperCase()).toUpperCase())) {
                    sourceInfoNum = getIDataInfoService(sourceType)
                            .fieldNumberSum(contrastInfo.getSourceCode(), field, contrastInfo.getSourceSchema(), contrastInfo.getSourceTable(), sourceWhereCondition);
                    String targetField = fieldsNameMap.get(field.toUpperCase());
                    targetInfoNum = getIDataInfoService(contrastType)
                            .fieldNumberSum(contrastInfo.getContrastCode(), targetField, contrastInfo.getContrastSchema(), contrastInfo.getContrastTable(), targetWhereCondition);
                    log.info("对" + field + "字段sum比较结果为" + (sourceInfoNum.equals(targetInfoNum) == true ? "成功" : "失败"));
                    flag = sourceInfoNum.equals(targetInfoNum);
                    if (!flag) break;
                }
            }
            return flag;
        } catch (Exception e) {
            log.error("数值sum: {}", e.getMessage(), e);
            return false;
        }
    }

    /**
     * @param contrastInfo
     * @Method distinctCountComparison
     * @Param
     * @Return java.lang.Boolean
     * @Description 去重Count对比
     * @Author Wang zhihao
     * @Date 2023/8/17 20:08
     * @Version V1.0
     */
    public CrossDbContrastSingleResultDto distinctCountComparison(BasicContrastInfo contrastInfo, CrossDbContrastDataSummaryDto contrastDataSummary) {
        try {
            ArrayList<String> failResultFields = new ArrayList<>();
            CrossDbContrastSingleResultDto singleResultDto = new CrossDbContrastSingleResultDto(false, "");
            for (int i = 0; i < contrastInfo.getField().size(); i++) {
                List<SourceConfInfoPo> sourceConfInfoPoList = sourceConfInfoService.queryListBySourceCode(contrastInfo.getSourceCode());
                List<SourceConfInfoPo> contrastConfInfoPoList = sourceConfInfoService.queryListBySourceCode(contrastInfo.getContrastCode());
                CrossDbContrastCommonDto crossDbContrastCommonDto = new CrossDbContrastCommonDto();
                BeanUtils.copyProperties(contrastInfo, crossDbContrastCommonDto);
                CrossDbContrastDataSqlDto dataSqlDto = crossDbContrastCommonService.buildPartitionWhereSql(sourceConfInfoPoList.get(0).getSourceType(), contrastConfInfoPoList.get(0).getSourceType(), crossDbContrastCommonDto);
                String sourceWhereCondition = initWhereCondition(dataSqlDto, contrastDataSummary, SOURCE_WHERE);
                String targetWhereCondition = initWhereCondition(dataSqlDto, contrastDataSummary, TARGET_WHERE);
                Long sourceInfoDistinct = getIDataInfoService(sourceConfInfoPoList.get(0).getSourceType())
                        .selectDistinctCount(contrastInfo.getSourceCode(), contrastInfo.getSourceSchema(), contrastInfo.getSourceTable(), contrastInfo.getField().get(i), sourceWhereCondition);
                Long targetInfoDistinct = getIDataInfoService(contrastConfInfoPoList.get(0).getSourceType())
                        .selectDistinctCount(contrastInfo.getContrastCode(), contrastInfo.getContrastSchema(), contrastInfo.getContrastTable(), contrastInfo.getContrastField().get(i), targetWhereCondition);
                if (!sourceInfoDistinct.equals(targetInfoDistinct)) {
                    failResultFields.add(contrastInfo.getField().get(i));
                }
            }
            contrastLongResult(failResultFields, singleResultDto);
            return singleResultDto;
        } catch (Exception e) {
            log.error("去重Count对比发生异常: {}", e.getMessage(), e);
            Throwable rootCause = crossDbContrastCommonService.findRootCause(e);
            return new CrossDbContrastSingleResultDto(false, rootCause.getMessage());
        }
    }

    /**
     * @param contrastInfo
     * @Method groupCountComparison
     * @Param
     * @Return java.lang.Boolean
     * @Description 分组Count对比
     * @Author Wang zhihao
     * @Date 2023/8/17 20:21
     * @Version V1.0
     */
    public CrossDbContrastSingleResultDto groupCountComparison(BasicContrastInfo contrastInfo, CrossDbContrastDataSummaryDto contrastDataSummary) {
        try {
            ArrayList<String> failResultFields = new ArrayList<>();
            CrossDbContrastSingleResultDto singleResultDto = new CrossDbContrastSingleResultDto(false, "");
            for (int i = 0; i < contrastInfo.getField().size(); i++) {
                List<SourceConfInfoPo> sourceConfInfoPoList = sourceConfInfoService.queryListBySourceCode(contrastInfo.getSourceCode());
                List<SourceConfInfoPo> contrastConfInfoPoList = sourceConfInfoService.queryListBySourceCode(contrastInfo.getContrastCode());
                CrossDbContrastCommonDto crossDbContrastCommonDto = new CrossDbContrastCommonDto();
                BeanUtils.copyProperties(contrastInfo, crossDbContrastCommonDto);
                CrossDbContrastDataSqlDto dataSqlDto = crossDbContrastCommonService.buildPartitionWhereSql(sourceConfInfoPoList.get(0).getSourceType(), contrastConfInfoPoList.get(0).getSourceType(), crossDbContrastCommonDto);
                String sourceWhereCondition = initWhereCondition(dataSqlDto, contrastDataSummary, SOURCE_WHERE);
                String targetWhereCondition = initWhereCondition(dataSqlDto, contrastDataSummary, TARGET_WHERE);
                Long sourceInfoGroup = getIDataInfoService(sourceConfInfoPoList.get(0).getSourceType())
                        .selectGroupCount(contrastInfo.getSourceCode(), contrastInfo.getSourceSchema(), contrastInfo.getSourceTable(), contrastInfo.getField().get(i), sourceWhereCondition);
                Long targetInfoGroup = getIDataInfoService(contrastConfInfoPoList.get(0).getSourceType())
                        .selectGroupCount(contrastInfo.getContrastCode(), contrastInfo.getContrastSchema(), contrastInfo.getContrastTable(), contrastInfo.getContrastField().get(i), targetWhereCondition);
                if (!sourceInfoGroup.equals(targetInfoGroup)) {
                    failResultFields.add(contrastInfo.getField().get(i));
                }
            }
            contrastLongResult(failResultFields, singleResultDto);
            return singleResultDto;
        } catch (Exception e) {
            log.error("分组Count对比发生异常: {}", e.getMessage(), e);
            Throwable rootCause = crossDbContrastCommonService.findRootCause(e);
            return new CrossDbContrastSingleResultDto(false, rootCause.getMessage());
        }
    }

    /**
     * @param contrastInfo
     * @Method fieldCount
     * @Param
     * @Return java.lang.Boolean
     * @Description 字段Count对比
     * @Author Wang zhihao
     * @Date 2023/8/17 20:31
     * @Version V1.0
     */
    public CrossDbContrastSingleResultDto fieldCount(BasicContrastInfo contrastInfo, CrossDbContrastDataSummaryDto contrastDataSummary) {
        ArrayList<String> failResultFields = new ArrayList<>();
        CrossDbContrastSingleResultDto singleResultDto = new CrossDbContrastSingleResultDto(false, "");
        try {
            for (int i = 0; i < contrastInfo.getField().size(); i++) {
                List<SourceConfInfoPo> sourceConfInfoPoList = sourceConfInfoService.queryListBySourceCode(contrastInfo.getSourceCode());
                List<SourceConfInfoPo> contrastConfInfoPoList = sourceConfInfoService.queryListBySourceCode(contrastInfo.getContrastCode());
                CrossDbContrastCommonDto crossDbContrastCommonDto = new CrossDbContrastCommonDto();
                BeanUtils.copyProperties(contrastInfo, crossDbContrastCommonDto);
                CrossDbContrastDataSqlDto dataSqlDto = crossDbContrastCommonService.buildPartitionWhereSql(sourceConfInfoPoList.get(0).getSourceType(), contrastConfInfoPoList.get(0).getSourceType(), crossDbContrastCommonDto);
                String sourceWhereCondition = initWhereCondition(dataSqlDto, contrastDataSummary, SOURCE_WHERE);
                String targetWhereCondition = initWhereCondition(dataSqlDto, contrastDataSummary, TARGET_WHERE);
                List<Map<String, String>> sourceInfoCount = getIDataInfoService(sourceConfInfoPoList.get(0).getSourceType())
                        .selectFieldCount(contrastInfo.getSourceCode(), contrastInfo.getSourceSchema(), contrastInfo.getSourceTable(), contrastInfo.getField().get(i), sourceWhereCondition);
                List<Map<String, String>> targetInfoCount = getIDataInfoService(contrastConfInfoPoList.get(0).getSourceType())
                        .selectFieldCount(contrastInfo.getContrastCode(), contrastInfo.getContrastSchema(), contrastInfo.getContrastTable(), contrastInfo.getField().get(i), targetWhereCondition);
                //比较两个List （排序）
                Set<Map<String, String>> set1 = new TreeSet<>(Comparator.comparing(Object::hashCode));
                Set<Map<String, String>> set2 = new TreeSet<>(Comparator.comparing(Object::hashCode));
                set1.addAll(sourceInfoCount);
                set2.addAll(targetInfoCount);
                //对比结果处理
                if (!CollectionUtils.isEqualCollection(set1, set2)) {
                    failResultFields.add(contrastInfo.getField().get(i));
                }
            }
            contrastLongResult(failResultFields, singleResultDto);
            return singleResultDto;
        } catch (Exception e) {
            Throwable rootCause = crossDbContrastCommonService.findRootCause(e);
            log.error("字段Count对比发生异常: {}", e.getMessage());
            return new CrossDbContrastSingleResultDto(false, rootCause.getMessage());
        }
    }

    /**
     * @param contrastInfo
     * @Method sumField
     * @Param
     * @Return java.lang.Boolean
     * @Description 字段Sum对比
     * @Author Wang zhihao
     * @Date 2023/8/17 20:37
     * @Version V1.0
     */
    public CrossDbContrastSingleResultDto sumField(BasicContrastInfo contrastInfo, CrossDbContrastDataSummaryDto contrastDataSummary) {
        try {
            ArrayList<String> failResultFields = new ArrayList<>();
            CrossDbContrastSingleResultDto singleResultDto = new CrossDbContrastSingleResultDto(false, "");
            for (int i = 0; i < contrastInfo.getField().size(); i++) {
                List<SourceConfInfoPo> sourceConfInfoPoList = sourceConfInfoService.queryListBySourceCode(contrastInfo.getSourceCode());
                List<SourceConfInfoPo> contrastConfInfoPoList = sourceConfInfoService.queryListBySourceCode(contrastInfo.getContrastCode());
                CrossDbContrastCommonDto crossDbContrastCommonDto = new CrossDbContrastCommonDto();
                BeanUtils.copyProperties(contrastInfo, crossDbContrastCommonDto);
                CrossDbContrastDataSqlDto dataSqlDto = crossDbContrastCommonService.buildPartitionWhereSql(sourceConfInfoPoList.get(0).getSourceType(), contrastConfInfoPoList.get(0).getSourceType(), crossDbContrastCommonDto);
                String sourceWhereCondition = initWhereCondition(dataSqlDto, contrastDataSummary, SOURCE_WHERE);
                String targetWhereCondition = initWhereCondition(dataSqlDto, contrastDataSummary, TARGET_WHERE);
                Long sourceInfoSum = getIDataInfoService(sourceConfInfoPoList.get(0).getSourceType())
                        .selectFieldSum(contrastInfo.getSourceCode(), contrastInfo.getSourceSchema(), contrastInfo.getSourceTable(), contrastInfo.getField().get(i), sourceWhereCondition);
                Long targetInfoSum = getIDataInfoService(contrastConfInfoPoList.get(0).getSourceType())
                        .selectFieldSum(contrastInfo.getContrastCode(), contrastInfo.getContrastSchema(), contrastInfo.getContrastTable(), contrastInfo.getField().get(i), targetWhereCondition);
                if (!sourceInfoSum.equals(targetInfoSum)) {
                    failResultFields.add(contrastInfo.getField().get(i));
                }
            }
            contrastLongResult(failResultFields, singleResultDto);
            return singleResultDto;
        } catch (Exception e) {
            log.error("字段Sum对比发生异常: {}", e.getMessage(), e);
            Throwable rootCause = crossDbContrastCommonService.findRootCause(e);
            return new CrossDbContrastSingleResultDto(false, rootCause.getMessage());
        }
    }

    /**
     * @param contrastInfo
     * @Method fieldTrim
     * @Param
     * @Return java.lang.Boolean
     * @Description 字段Trim对比
     * @Author Wang zhihao
     * @Date 2023/8/17 20:43
     * @Version V1.0
     */
    public CrossDbContrastSingleResultDto fieldTrim(BasicContrastInfo contrastInfo, CrossDbContrastDataSummaryDto contrastDataSummary) {
        try {
            ArrayList<String> failResultFields = new ArrayList<>();
            CrossDbContrastSingleResultDto singleResultDto = new CrossDbContrastSingleResultDto(false, "");
            for (int i = 0; i < contrastInfo.getField().size(); i++) {
                List<SourceConfInfoPo> sourceConfInfoPoList = sourceConfInfoService.queryListBySourceCode(contrastInfo.getSourceCode());
                List<SourceConfInfoPo> contrastConfInfoPoList = sourceConfInfoService.queryListBySourceCode(contrastInfo.getContrastCode());
                CrossDbContrastCommonDto crossDbContrastCommonDto = new CrossDbContrastCommonDto();
                BeanUtils.copyProperties(contrastInfo, crossDbContrastCommonDto);
                CrossDbContrastDataSqlDto dataSqlDto = crossDbContrastCommonService.buildPartitionWhereSql(sourceConfInfoPoList.get(0).getSourceType(), contrastConfInfoPoList.get(0).getSourceType(), crossDbContrastCommonDto);
                String sourceWhereCondition = initWhereCondition(dataSqlDto, contrastDataSummary, SOURCE_WHERE);
                String targetWhereCondition = initWhereCondition(dataSqlDto, contrastDataSummary, TARGET_WHERE);
                List<String> sourceInfoTrim = getIDataInfoService(sourceConfInfoPoList.get(0).getSourceType())
                        .selectFieldTrim(contrastInfo.getSourceCode(), contrastInfo.getSourceSchema(), contrastInfo.getSourceTable(), contrastInfo.getField().get(i), sourceWhereCondition);
                List<String> targetInfoTrim = getIDataInfoService(contrastConfInfoPoList.get(0).getSourceType())
                        .selectFieldTrim(contrastInfo.getContrastCode(), contrastInfo.getContrastSchema(), contrastInfo.getContrastTable(), contrastInfo.getField().get(i), targetWhereCondition);
                if (!CollectionUtils.isEqualCollection(sourceInfoTrim, targetInfoTrim)) {
                    failResultFields.add(contrastInfo.getField().get(i));
                }
            }
            contrastLongResult(failResultFields, singleResultDto);
            return singleResultDto;
        } catch (Exception e) {
            log.error("字段Trim对比发生异常: {}", e.getMessage(), e);
            Throwable rootCause = crossDbContrastCommonService.findRootCause(e);
            return new CrossDbContrastSingleResultDto(false, rootCause.getMessage());
        }
    }

    /**
     * @Method fieldCountIsNull
     * @Param
     * @Param contrastInfo
     * @Return java.lang.Boolean
     * @Description 空字段Count
     * @Author xin.zhao
     * @Date 2023/9/27 20:12
     * @Version V1.0
     */
    @Override
    public CrossDbContrastSingleResultDto fieldCountIsNull(BasicContrastInfo contrastInfo, CrossDbContrastDataSummaryDto contrastDataSummary) {
        try {
            //比对失败集合
            ArrayList<String> failResultFields = new ArrayList<>();
            //比对结果类
            CrossDbContrastSingleResultDto singleResultDto = new CrossDbContrastSingleResultDto(false, "");
            //
            for (int i = 0; i < contrastInfo.getField().size(); i++) {
                List<SourceConfInfoPo> sourceConfInfoPoList = sourceConfInfoService.queryListBySourceCode(contrastInfo.getSourceCode());
                List<SourceConfInfoPo> contrastConfInfoPoList = sourceConfInfoService.queryListBySourceCode(contrastInfo.getContrastCode());
                CrossDbContrastCommonDto crossDbContrastCommonDto = new CrossDbContrastCommonDto();
                BeanUtils.copyProperties(contrastInfo, crossDbContrastCommonDto);
                CrossDbContrastDataSqlDto dataSqlDto = crossDbContrastCommonService.buildPartitionWhereSql(sourceConfInfoPoList.get(0).getSourceType(), contrastConfInfoPoList.get(0).getSourceType(), crossDbContrastCommonDto);
                String sourceWhereCondition = initWhereCondition(dataSqlDto, contrastDataSummary, SOURCE_WHERE);
                String targetWhereCondition = initWhereCondition(dataSqlDto, contrastDataSummary, TARGET_WHERE);
                Long sourceInfoDistinct = getIDataInfoService(sourceConfInfoPoList.get(0).getSourceType())
                        .selectCountSumIsNull(contrastInfo.getSourceCode(), contrastInfo.getSourceSchema(), contrastInfo.getSourceTable(), contrastInfo.getField().get(i), sourceWhereCondition);
                Long targetInfoDistinct = getIDataInfoService(contrastConfInfoPoList.get(0).getSourceType())
                        .selectCountSumIsNull(contrastInfo.getContrastCode(), contrastInfo.getContrastSchema(), contrastInfo.getContrastTable(), contrastInfo.getContrastField().get(i), targetWhereCondition);
                if (!sourceInfoDistinct.equals(targetInfoDistinct)) {
                    failResultFields.add(contrastInfo.getField().get(i));
                }
            }
            contrastLongResult(failResultFields, singleResultDto);
            return singleResultDto;
        } catch (Exception e) {
            log.error("去重Count对比发生异常: {}", e.getMessage(), e);
            Throwable rootCause = crossDbContrastCommonService.findRootCause(e);
            return new CrossDbContrastSingleResultDto(false, rootCause.getMessage());
        }
    }

    /**
     * @param originalName
     * @param
     * @Method getFieldNameByMap
     * @Param
     * @Return java.lang.String
     * @Description 根据比对项所关联的表迁移的字段映射获取比对源所对应的字段
     * @Author Wang zhihao
     * @Date 2023/8/29 20:36
     * @Version V1.0
     */
    public String getFieldNameByMap(String originalName, CrossDbContrastCommonDto contrastInfo) {
        List<TableTransferFieldMapResponseDto> tableTransferFieldMaps = new ArrayList<>();
        if (contrastInfo.getTableTransferPkId() != null) {
            //如果经历过表迁移，那么就从表迁移里获取字段映射
            TableTransferFieldMapRequestDto tableTransferFieldMapRequestDto = new TableTransferFieldMapRequestDto();
            tableTransferFieldMapRequestDto.setListPkId(contrastInfo.getTableTransferPkId()).setSourceFieldName(originalName);
            tableTransferFieldMaps = tableTransferFieldMapService.queryList(tableTransferFieldMapRequestDto);
            if (!tableTransferFieldMaps.isEmpty()) {
                return tableTransferFieldMaps.get(0).getTargetFieldName();
            }
        } else {
            //否则就连接表获取字段映射
            tableTransferFieldMaps = sourceInfoService.getFieldMaps(converter.toBasicTransferInfo(contrastInfo));
            for (TableTransferFieldMapResponseDto tableTransferFieldMap : tableTransferFieldMaps) {
                if (tableTransferFieldMap.getSourceFieldName().equals(originalName))
                    return tableTransferFieldMap.getTargetFieldName();
            }

        }
        return originalName;
    }

    /**
     * @Method getContrastMessage
     * @Param
     * @Param singleResultDto
     * @Param sourceMap
     * @Param targetMap
     * @Param set1
     * @Param set2
     * @Param contrastType
     * @Return void
     * @Description 比较set 获取比对差异信息
     * @Author xin.zhao
     * @Date 2023/11/16 14:28
     * @Version V1.0
     */
    public void getContrastMessage(CrossDbContrastSingleResultDto singleResultDto, HashMap<String, String> sourceMap, HashMap<String, String> targetMap, Set<Map<String, String>> set1, Set<Map<String, String>> set2, String contrastType) {
        Iterator<Map<String, String>> iterator = set1.iterator();
        while (iterator.hasNext()) {
            Map<String, String> next = iterator.next();
            String fieldName = next.get(COLUMN_NAME);
            if (!set2.contains(next)) {
                set1.stream()
                        .filter(map -> fieldName.equals(map.get(COLUMN_NAME)))
                        .forEach(source -> sourceMap.put(source.get(COLUMN_NAME), source.get(contrastType)));
                // 找到set2中匹配的项，将其添加到targetMap
                set2.stream()
                        .filter(map -> fieldName.equals(map.get(COLUMN_NAME)))
                        .forEach(target -> targetMap.put(target.get(COLUMN_NAME), target.get(contrastType)));
            }
        }
        if (sourceMap.isEmpty()) {
            singleResultDto.setMessage("");
        } else {
            singleResultDto.setResult(false);
            singleResultDto.setMessage(sourceMap.toString() + targetMap.toString());
        }
    }

    public void contrastLongResult(ArrayList<String> failResultFields, CrossDbContrastSingleResultDto singleResultDto) {
        if (CollUtil.isEmpty(failResultFields)) {
            singleResultDto.setResult(true).setMessage("比对成功");
        } else singleResultDto.setMessage("比对错误字段为：" + failResultFields.toString());
    }

    private String getWhereSql(String partitionWhereSql, String whereCondition) {
        if (StringUtils.isNotBlank(partitionWhereSql)) {
            if (StringUtils.isNotBlank(whereCondition)) {
                return partitionWhereSql + " and " + whereCondition;
            } else {
                return partitionWhereSql;
            }
        } else {
            if (com.hex.bigdata.hbda.utils.StringUtils.isNotBlank(whereCondition)) {
                return "where " + whereCondition;
            } else {
                return "";
            }
        }
    }

}
